import * as Components from "../../game/entitas/component";
import * as Ecs from "framework/entitas/index"
import {GameMatcher} from "./GameMatcher";

export class <%=ContextName%>Entity extends Ecs.Entity
{
<%
var components = contextData.comps
for (var i = 0; i < components.length; i++) {
    var c = components[i]
    if(!c.isSimple){
%>
    public <%=c.name%> : Components.<%=c.Name%>
    static _<%=c.Name%>ComponentPool = new Ecs.Bag();

    public get has<%=c.Name%>() : boolean
    {
        return this.hasComponent('<%=c.name%>')
    }

    public add<%=c.Name%> (val) : GameEntity
    {
        var comp : any = GameEntity._<%=c.Name%>ComponentPool.size() > 0 ? GameEntity._<%=c.Name%>ComponentPool.removeLast() :  new Components.<%=c.Name%>();
        comp.val = val
        this.addComponent(comp);
        return this
    }

    public replace<%=c.Name%> (val)
    {
        var preComp = this.has<%=c.Name%> ? this.<%=c.name%> : null;
        var comp : any = GameEntity._<%=c.Name%>ComponentPool.size() > 0 ? GameEntity._<%=c.Name%>ComponentPool.removeLast() :  new Components.<%=c.Name%>();
        comp.val = val
        this.replaceComponent('<%=c.name%>', comp);
        if (preComp != null) {
        GameEntity._<%=c.Name%>ComponentPool.add(preComp);
        }
        return this;
    }

    public remove<%=c.Name%> ()
    {
        var comp = this.<%=c.name%>;
        this.removeComponent('<%=c.name%>');
        GameEntity._<%=c.Name%>ComponentPool.add(comp);
        return this
    }

<%}else{%>
    static _<%=c.Name%>ComponentPool = new Ecs.Bag();
    public has<%=c.Name%>() : boolean
    {
        return this.hasComponent('<%=c.name%>')
    }

    public set<%=c.Name%>(v) : GameEntity
    {
        if (this.has<%=c.Name%>() != v)
        {
            if (v)
            {
                var comp : any = GameEntity._<%=c.Name%>ComponentPool.size() > 0 ? GameEntity._<%=c.Name%>ComponentPool.removeLast() : new Components.<%=c.Name%>();
                this.addComponent(comp)
            }
            else
            {
                this.removeComponent('<%=c.Name%>')
            }
        }
        return this
    }
<%}}%>
}
